/**
 *
 * Created by Pan,hongxue, 9/16/2011 11:29:02 AM
 * Enhanced by Song, YeWen, 11/2/2011 11:14:23 AM
 *
 */

function validateVideoUrl(ori_url) 
{
	var html = download_page(ori_url,'GBK');
	var matches = html.match(/[_]?vid\s*=\s*"(\d+)"/);
	if (!matches) {
		// Not Found
		matches = html.match(/[_]?vid\s*=\s*(\d+)/);
		if (!matches)
			return 'NotFoundVideo';
	}

	var dst_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid=' + matches[1];
 	if(ori_url.indexOf("my.tv.sohu.com")){
            dst_url = 'http://my.tv.sohu.com/videinfo.jhtml?m=viewtv&vid=' + matches[1];
        }

	var json_str = download_page(dst_url);
	var json = eval('(' + json_str + ')');
	var sb = [];

	if (json && json.data) {
		var files = json.data.su;
		for (var i=0; i<files.length; i++) {
			sb.push('http://' + json.allot + '/?pr-ot=2&new=' + files[i]);
		}
	}

        if(sb.length){
           //matches = html.match(/<title>(.*?)<\/title>/);
           matches = html.match(/<title>([\s\S]*?)<\/title>/);
           if(matches){
              return 'success_title:' + encodeURIComponent(matches[1])
                     + '\n video_urls:' + encodeURIComponent(sb)
                     + '\n seconds:' + Math.round(parseInt(json.data.totalDuration));
           }
        }
	return 'error:empty_path';
}

// http://tv.sohu.com/20111102/n324191828.shtml
function js_get_media_url(ori_url) 
{
	var html = download_page(ori_url);
	var matches = html.match(/[_]?vid\s*=\s*"(\d+)"/);
	if (!matches) {
		// Not Found
		//try to detect: "http://msn.tv.sohu.com/20111101/n324181839.shtml"
		matches = html.match(/[_]?vid\s*=\s*(\d+)/);
		if (!matches)
			return '';
	}

	var dst_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid=' + matches[1];
	if(ori_url.indexOf("my.tv.sohu.com")){
            dst_url = 'http://my.tv.sohu.com/videinfo.jhtml?m=viewtv&vid=' + matches[1];
        }

	var json_str = download_page(dst_url);
	var json = eval('(' + json_str + ')');
	var sb = [];

	if (json && json.data) {
		var files = json.data.su;
		for (var i=0; i<files.length; i++) {
			sb.push('http://' + json.allot + '/?pr-ot=2&new=' + files[i]);
		}
	}
	return sb;
}
